package com.jadefashion.controller;

import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.health.resources.QiniuUtils;
import com.jadefashion.health.resources.RedisConstant;
import com.jadefashion.health.resources.UploadUtils;
import com.jadefashion.pojo.Setmeal;
import com.jadefashion.service.SetmealService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("updateSetmeal.do")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Object updateSetmeal(@RequestParam("checkgroupIds") Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        Result result = null;
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.del("setmeal","setmealList");
            setmealService.updateSetmeal(checkgroupIds, setmeal);
            result = new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
        return result;
    }

    @RequestMapping("deleteMealById.do")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Object deleteMealById(@RequestParam("id") Integer id){
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.del("setmeal","setmealList");
            setmealService.deleteMealById(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(true,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    @RequestMapping("findCheckGroupIds.do")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Object findCheckGroupIds(@RequestParam("id")Integer id){
        Result result= null;
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.del("setmeal","setmealList");
            Integer[] ids= setmealService.findCheckItemIdsByGroupId(id);
            result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,ids);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return result;
    }
    @RequestMapping("findPage.do")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public Object findPage(@RequestBody QueryPageBean queryPageBean){
        Result result= null;
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.del("setmeal","setmealList");
            Map<String,Object> map= setmealService.findPage(queryPageBean);
            result = new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
        return result;
    }

    @RequestMapping("upload.do")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Object upload(@RequestParam("imgFile") MultipartFile file) {
        Result result=null;
        String fileName = UploadUtils.generateRandonFileName(file.getOriginalFilename());
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            QiniuUtils.upload2Qiniu(file.getBytes(),fileName);
            resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            result=new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            result=new Result(true, MessageConstant.PIC_UPLOAD_FAIL);
            e.printStackTrace();
        }finally {
            resource.close();
        }
        return result;
    }

    @RequestMapping("add.do")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Object add(@RequestParam("checkgroupIds") Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        Result result = null;
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.del("setmeal","setmealList");
            setmealService.add(checkgroupIds, setmeal);
            result = new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return result;
    }

}
